/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} targetSum
 * @return {boolean}
 */
var hasPathSum = function (root, targetSum) {
  if (root == null) return false

  function getSum(root, sum) {
    sum += root.val
    if (!root.left && !root.right) return sum == targetSum
    let l = false,
      r = false
    if (root.left) {
      l = getSum(root.left, sum)
    }
    if (root.right) {
      r = getSum(root.right, sum)
    }
    return l || r
  }
  return getSum(root, 0)
};